package com.dgut.coal.controller;

import com.dgut.coal.entity.Info;
import com.dgut.coal.service.InfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author: Yang
 * @Description:
 * @Date: Created in 20:46 2019/9/28
 * @Modifued BY:
 */

@Controller
public class InfoController {

    @Autowired
    InfoService infoService;


    /**
     ** 前往资讯列表界面，显示所有资讯信息
     */
    @RequestMapping("/infolist")
    public String infoList(@RequestParam(required = false,defaultValue = "1",value = "pn") Integer pageNum, @RequestParam(required = false, defaultValue = "", value = "notify_id") Integer notify_id,@RequestParam(required = false, defaultValue = "", value = "title") String title, @RequestParam(required = false, defaultValue = "", value = "state") Integer state, Map<String, Object> map) {

        PageHelper.startPage(pageNum, 5);
        List<Info> list = infoService.infolist(notify_id, title, state);
        PageInfo pageInfo = new PageInfo<>(list, 5);
        List keywords = Arrays.asList(notify_id, title, state);
        map.put("keywords", keywords);
        map.put("pageInfo", pageInfo);
        return "information-list";
    }

    /**
     ** 根据id删除信息
     */
    @RequestMapping("/deleteInfo")
    @ResponseBody
    public int delete(Integer id){
        int row = infoService.deleteInfo(id);
        return row;
    }

    /**
     * 批量删除信息
     */
    @RequestMapping("/deleteMany")
    @ResponseBody
    public int deleteMany(String ids){
        String[] id = ids.split(",");
        int[] ints = new int[id.length];
        for (int i = 0; i < id.length; i++) {
            ints[i] = Integer.parseInt(id[i]);
        }
        int row = infoService.deleteMany(ints);
        return row;
    }

    /**
    * 将信息保存为草稿
     */
    @RequestMapping("/temp")
    @ResponseBody
    public int tempinfo(String title, String content,String create_user){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = df.format(new Date());
        int state = 1;
        int row = infoService.temp(create_user,format,title,content,state);
        return row;
    }

    /**
    * 添加公告时就将信息提交为待审核状态
     */
    @RequestMapping("/save")
    @ResponseBody
    public int saveinfo(String title, String content,String create_user){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = df.format(new Date());

        int state = 5;
        int row = infoService.save(create_user,format,title,content,state);
        return row;
    }


    /**
    ** 前往添加公告页面
     */
    @RequestMapping("/infoadd")
    public String infoAdd() {
        return "information-add";
    }

    /**
    ** 查看公告详情
     */
    @RequestMapping("/infoview")
    public String infoview(Integer notify_id, Integer state,Map<String, Object> map){
        Info ainfo = infoService.ainfo(notify_id);
        map.put("state",state);
        map.put("ainfo",ainfo);
        return "information-view";
    }

    /**
    ** 前往编辑公告页面
     */
    @RequestMapping("/edit")
    public String editpage(Integer notify_id,Map<String, Object> map){
        Info info = infoService.ainfo(notify_id);
        map.put("info",info);
        return "information-edit";
    }

    /**
    * 编辑公告，将公告修改完之后保存为草稿
     */
    @RequestMapping("/update")
    @ResponseBody
    public int edit(String title, String content,Integer notify_id){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = df.format(new Date());
        int row = infoService.edit(title,content,format,notify_id);
        return row;
    }

    /**
     * 修改公告，修改完成后直接将信息提交为待审核
     * @param notify_id
     * @return
     */
    @RequestMapping("/updatesave")
    @ResponseBody
    public int updatesave(String title, String content,Integer notify_id){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = df.format(new Date());
        int state = 5;
        int row = infoService.updatesave(title,content,format,state,notify_id);
        return row;
    }

    /**
    ** 发布公告
     */
    @RequestMapping("/release")
    @ResponseBody
    public int release(Integer id){
        int row = infoService.release(id);
        return row;
    }

    /**
     * 进入资讯审核页面
     */

    @RequestMapping("/auditList")
    public String auditList(@RequestParam(required = false,defaultValue = "1",value = "pn") Integer pageNum, @RequestParam(required = false,defaultValue = "",value = "notify_id") Integer notify_id, @RequestParam(required = false,defaultValue = "",value = "title") String title,Map<String, Object> map){
        PageHelper.startPage(pageNum,5);
        List<Info> list = infoService.auditInfo(notify_id,title);
        PageInfo pageInfo = new PageInfo<>(list,5);
        List keywords = Arrays.asList(notify_id,title);
        map.put("keywords",keywords);
        map.put("pageInfo",pageInfo);
        return "information-audit-list";
    }

    @RequestMapping("/infoAudit")
    public String infoAudit(Integer notify_id,Map<String, Object> map){
        Info info = infoService.ainfo(notify_id);
        map.put("info",info);
        return "information-audit";

    }

    @RequestMapping("/pass")
    @ResponseBody
    public int pass(Integer notify_id,String audit_user){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String audit_date = format.format(new Date());
        int row = infoService.pass(notify_id,audit_user,audit_date);
        return row;
    }

    @RequestMapping("/reject")
    @ResponseBody
    public int reject(Integer notify_id,String audit_user){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String audit_date = format.format(new Date());
        int row = infoService.reject(notify_id,audit_user,audit_date);
        return row;
    }
}
